package cn.zhoutao.system.controller;

import cn.zhoutao.basic.util.AjaxResult;
import cn.zhoutao.basic.util.PageList;
import cn.zhoutao.system.domain.DictionaryType;
import cn.zhoutao.system.query.DictionaryTypeQuery;
import cn.zhoutao.system.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ddt")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;
    /**
     * 查询一条记录 GET
     */
    @GetMapping("/{id}")
    public DictionaryType queryById(@PathVariable("id") Long id){
         return  dictionaryTypeService.queryById(id);
    }
    /**
     * 查询所有 GET
     */
    @GetMapping
    public  List<DictionaryType> queryAll(){
         return dictionaryTypeService.queryAll();
    }
    /**
     * 新增和修改 put
     */
    @PutMapping
    public AjaxResult addOrUpdate (@RequestBody DictionaryType dictionaryType){
        try {
        if (dictionaryType.getId() == null){
            dictionaryTypeService.save(dictionaryType);
        }else {
            dictionaryTypeService.update(dictionaryType);
        }
        return AjaxResult.me();
    }catch (Exception e){
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统wa了，我们正在殴打程序员");
        }
    }
    /**
     * 删除一条记录 DELTTE
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dictionaryTypeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统wa了，我们正在殴打程序员");
        }
        }
    /**
     * 高级查询 + 分页 Post
     */
    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery query){
        return dictionaryTypeService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            dictionaryTypeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("系统wa了，我们正在殴打程序员");
        }
    }

}
